package cn.springcloud.fix.demo.leecode;

public class Test03 {

    public static void main(String[] args) {
        int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        int[] res = fun(nums, 6);
        for (int i : res) {
            System.out.print(i + "\t");
        }
    }

    private static int cal(int[] nums, int start, int end) {
        int sum = 0;
        for (int i = start; i < end; i++) {
            sum = sum + nums[i];
        }
        return sum;
    }

    private static int[] fun(int[] nums, int L) {
        if (nums.length < L) return new int[]{};
        if (nums.length == L) return nums;
        int index = 0;
        int max = 0;
        for (int i = L; i < nums.length; i++) {
            int sum = cal(nums, i - L, i);
            if (sum >= max) {
                max = sum;
                index = i - L;
            }
        }
        int[] res = new int[L];
        //for (int i = index; i < index + L; i++) {
        //    res[i - index] = nums[i];
        //}
        System.arraycopy(nums, index, res, 0, L);
        return res;
    }
}
